/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package examenDAO;

import java.util.ArrayList;
import java.util.List;
import examenTO.alumnoTo;
import examenUtil.DBConn;

/**
 *
 * @author Alumno
 */
public class alumnoDao extends DBConn{
    public int insertarDatosAlumno(alumnoTo to) {
        int r=0;
        try {
            getConexionDb();
           ps=con.prepareStatement("insert into "
                   +"alumno.alumno(nombre, apellpat, apellmat, direcion, telefono, email, dni, codigo) "
                   + "values (?, ?, ?, ?, ?, ?, ?, ?)"); 
            
            ps.setString(1, to.getNombre());
            ps.setString(2, to.getApellmat());
            ps.setString(3, to.getApellpat());
            ps.setString(4, to.getDireccion());
            ps.setString(5, to.getTelefono());
            ps.setString(6, to.getEmail());
            ps.setString(7, to.getDni());
            ps.setString(8, to.getCodigo());

            if (ps.executeUpdate() == 1) {
                r=1;    
                System.out.println("ha sido insertado");
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return r;    
    }  
    
    public List reporteDatoAlumno() {  
        List reporte = new ArrayList();
        alumnoTo to;
        try {
            getConexionDb();
           
            ps = con.prepareStatement("select * from alumno.alumno ");
            rs = ps.executeQuery();
            while (rs.next()) {
               to = new alumnoTo();
                to.setId_alumno(rs.getInt("id_alumno"));
                
                to.setNombre(rs.getString("nombre"));
                to.setApellpat(rs.getString("apellpat"));
                to.setApellmat(rs.getString("apellmat"));
                to.setDireccion(rs.getString("direccion"));
                to.setTelefono(rs.getString("telefono"));
                to.setEmail(rs.getString("email"));
                to.setDni(rs.getString("dni"));
                to.setCodigo(rs.getString("codigo"));

                reporte.add(to);
                
                System.out.println("Reportado..!");
            }
        } catch (Exception e) {
            System.out.println("ocurrio un error en el reporte" + e.getMessage());
        }
        finally {
         getCerrarConexion();
        }
        return reporte;

    }
    public void eliminarDatosAlumno(int id_alumno) {
        try {
            getConexionDb();
            ps=con.prepareStatement("Delete from alumno.alumno where id_alumno=? ");
            ps.setInt(1, id_alumno);
            
            if(ps.executeUpdate()==1){
                
                System.out.println("ha sido eliminado");
            }
        } catch (Exception e) {
        }
        finally{
            getCerrarConexion();
        }
    }
    public void actualizarDatosAlumno(alumnoTo to,int id_alumno ) {
        try {
             
            getConexionDb();
            ps=con.prepareStatement("update alumno.alumno set nombre=?, apellpat=?,"
                    + "apellmat=?, direccion=?, telefono=?, email=?, dni=?, codigo=?, where id_alumno=?");
            
            ps.setString(1, to.getNombre());
            ps.setString(2, to.getApellpat()); 
            ps.setString(3, to.getApellmat()); 
            ps.setString(4, to.getDireccion());
            ps.setString(5, to.getTelefono());
            ps.setString(6, to.getEmail());
            ps.setString(7, to.getDni());
            ps.setString(8, to.getCodigo());
            ps.setInt(9, id_alumno);
            
            ps.executeUpdate(); 
         
         System.out.println("se ha actualizado con exito");
          
        } catch (Exception e) {
        } finally {
            getCerrarConexion();
        }
    }
}




    
    
    
    
    

